<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="供应商名称" prop="supply_id">
                    <!-- <el-select v-model="queryParams.supply_id" placeholder="请选择供应商" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in supplyList" :key="index" :label="dict.supply_name" :value="dict.id" />
                    </el-select> -->
                    <SelectPage v-model="queryParams.supply_id" placeholder="请选择供应商" :url="$api.supply.getSupplyList" show_field="supply_name" query_field="supply_name" query_field2="spu" filterable clearable popper-class="custom-select" style="width: 240px" ></SelectPage>
                </el-form-item>
                <el-form-item label="SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="核心供应商" prop="is_head">
                    <el-select v-model="queryParams.is_head" placeholder="请选择核心供应商" clearable  style="width: 240px">
                        <el-option v-for="(dict, index) in dict.IS_HEAD_LIST" :key="index" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否1688" prop="is_1688">
                    <el-select v-model="queryParams.is_1688" placeholder="是否1688" clearable  style="width: 240px">
                        <el-option v-for="(dict, index) in dict.IS_HEAD_LIST" :key="index" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({})">添加</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="ID" prop="id" min-width="40"></el-table-column>
                <el-table-column label="供应商名称" prop="supply_name" min-width="200" show-overflow-tooltip sortable>
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showDetailDialogspuinfo(scope.row)">{{ scope.row.supply_name }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="是否1688" prop="is_1688" min-width="60">
                    <template slot-scope="scope">
                        {{ scope.row.is_1688 | formatLabelByValueFuzzy(dict.IS_HEAD_LIST) }}
                    </template>
                </el-table-column>

                <!-- <el-table-column label="供应商链接" prop="supply_link" min-width="200" show-overflow-tooltip></el-table-column> -->
                <el-table-column label="SPU" prop="spu" min-width="60" show-overflow-tooltip></el-table-column>
                <el-table-column label="状态" prop="status" min-width="60">
                    <template slot-scope="scope">
                        {{ scope.row.status | formatLabelByValueFuzzy(STATUS_LIST) }}
                    </template>
                </el-table-column>
                <el-table-column label="核心供应商" prop="is_head" min-width="60">
                    <template slot-scope="scope">
                        {{ scope.row.is_head | formatLabelByValueFuzzy(dict.IS_HEAD_LIST) }}
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="created_time" min-width="100"></el-table-column>
                <el-table-column label="操作" min-width="100">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-edit" @click="deleteItem(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery2"></detail-dialog>

        <DetailDialogspuinfo v-model="detailDialogspuinfo.visible" v-if="detailDialogspuinfo.visible" :params="detailDialogspuinfo.data" @success="handleQueryspuinfo"></DetailDialogspuinfo>
    </div>
</template>

<script>
import { parseTime } from '@/utils/quickadmin'
import { querySupplyList, querySupplySpuList, deleteSupplyList ,querySupplyListdown } from '@/api/supply'
import { STATUS_LIST, IS_HEAD_LIST } from '../constant'
import DetailDialog from '../components/DetailDialog.vue'
import DetailDialogspuinfo from '../components/DetailDialogspuinfo.vue'
export default {
    name: 'SupplyList',
    components: { DetailDialog,DetailDialogspuinfo },
    data() {
        return {
            supplyList: [],
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            STATUS_LIST,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                supply_id: undefined,
                supply_name: undefined,
                spu: undefined
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            detailDialogspuinfo: {
                visible: false,
                data: {},
            },
            dict: {
                STATUS_LIST,
                IS_HEAD_LIST,
                spuList: []
            }
        }
    },
    async created() {
        this.loading = true
        try {
            const spuData = await querySupplySpuList()
            this.dict.spuList = spuData?.data?.list ?? []

            const SupplyListData = await querySupplyListdown()
            this.supplyList = SupplyListData?.data ?? []

        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        showDetailDialogspuinfo(row) {
            this.detailDialogspuinfo.visible = true
            this.detailDialogspuinfo.data = row
        },
        deleteItem(row){
            this.$confirm('确认要删除该供应商吗?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(()=>{
                deleteSupplyList({id: row.id}).then((res)=>{
                    this.$message.success(res.msg)
                    this.getList()
                })
            })
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            querySupplyList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 搜索按钮操作
        handleQuery2() {
            this.getList()
        },
        handleQueryspuinfo() {
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
